<?php

// versione 0.1
// mancano ancora moltissime cose!!!

if (!defined('GMAPS_API_KEY')) {
	trigger_error("Definisci un'API KEY per il servizio mappe", E_USER_ERROR);
}

// CONTROLS
define ('GLargeMapControl', 'google.maps.LargeMapControl'); // a large pan/zoom control used on Google Maps. Appears in the top left corner of the map by default.
define ('GSmallMapControl', 'google.maps.SmallMapControl'); // a smaller pan/zoom control used on Google Maps. Appears in the top left corner of the map by default.
define ('GSmallZoomControl', 'google.maps.SmallZoomControl'); // a small zoom control (no panning controls) used in the small map blowup windows used to display driving directions steps on Google Maps.
define ('GScaleControl', 'google.maps.ScaleControl'); // a map scale
define ('GMapTypeControl', 'google.maps.MapTypeControl'); // buttons that let the user toggle between map types (such as Map and Satellite)
define ('GHierarchicalMapTypeControl', 'google.maps.HierarchicalMapTypeControl'); // a selection of nested buttons and menu items for placing many map type selectors.
define ('GOverviewMapControl', 'google.maps.OverviewMapControl'); // a collapsible overview map in the corner of the screen

class gmaps {
	
	var $out;
	var $domElId;
	var $markers = array();
	var $init_done = false;
	
	var $center_lat, $center_lng, $zoom;
	
	function gmaps($domElId) {
		$this->domElId = $domElId;
		$this->out  = '<script type="text/javascript" src="http://www.google.com/jsapi?key='. GMAPS_API_KEY .'"></script>'."\n";
		$this->out .= '<script type="text/javascript">'."\n";
  		$this->out .= 'google.load("maps", "2.x");'."\n";
  		$this->out .= 'function gmaps_initialize() {'."\n";
  		$this->out .= '	if (google.maps.BrowserIsCompatible()) {'."\n";
	}
	
	function add_map($lat, $lng, $zoom) {
		$this->center_lat = $lat;
		$this->center_lng = $lng;
		$this->zoom = $zoom;
		$this->out .= '		var map_'. $this->domElId .' = new google.maps.Map2(document.getElementById("'. $this->domElId .'"));'."\n";
		$this->out .= '		map_'. $this->domElId .'.setCenter(new google.maps.LatLng('. $lat .', '. $lng .'), '. $zoom .');'."\n";
	}
	
	function add_control($controlType) {
		$this->out .= '		map_'. $this->domElId .'.addControl(new '. $controlType .'());'."\n";
	}
	
	function add_marker($lat = false, $lng = false, $html = false, $infoWindowOpen = false) {
		
		if ($lat == false) $lat = $this->center_lat;
		if ($lng == false) $lng = $this->center_lng;
		
		if (count($this->markers) == 0) {
			$this->out .= '		function createMarker(lat, lng, html) {'."\n";
			$this->out .= '			var point = new google.maps.LatLng(lat, lng);'."\n";
			$this->out .= '			var marker = new google.maps.Marker(point);'."\n";
			$this->out .= '			if (html) {'."\n";
			$this->out .= '				google.maps.Event.addListener(marker, "click", function() {'."\n";
			$this->out .= '					marker.openInfoWindowHtml(html);'."\n";
			$this->out .= '				});'."\n";
			$this->out .= '			}'."\n";
			$this->out .= '			return marker;'."\n";
			$this->out .= '		}'."\n";
		}
		
		$this->out .= '		var marker_'. count($this->markers) .' = createMarker('. $lat .', '. $lng .', '. ($html ? "'".addslashes($html)."'" : 'false') .');'."\n";
		$this->out .= '		map_'. $this->domElId .'.addOverlay(marker_'. count($this->markers) .');'."\n";
		if ($infoWindowOpen) {
			$this->out .= '		google.maps.Event.trigger(marker_'. count($this->markers).', "click");'."\n";
		}
		
		$this->markers[] = array($lat, $lng);
	}
	
	function init() {
		$this->out .= '	}'."\n";
		$this->out .= '}'."\n";
		$this->out .= 'google.setOnLoadCallback(gmaps_initialize);'."\n";
		$this->out .= '</script>'."\n";
		
		$GLOBALS['Template']->set('includes', $this->out);
		$this->init_done = true;
	}
	
	function set_dragging($enable) {
		$this->_set_property($enable, 'Dragging');
	}
	
	function set_infowindow($enable) {
		$this->_set_property($enable, 'InfoWindow');
	}
	
	function set_dblclickzoom($enable) {
		$this->_set_property($enable, 'DoubleClickZoom');
	}
	
	function set_continuouszoom($enable) {
		$this->_set_property($enable, 'ContinuousZoom');
	}
	
	function set_googlebar($enable) {
		$this->_set_property($enable, 'GoogleBar');
	}
	
	function set_scrollwheelzoom($enable) {
		$this->_set_property($enable, 'ScrollWheelZoom');
	}
	
	function _set_property ($enable, $property) {
		$this->out .= '	map_'. $this->domElId .'.'. ($enable ? 'enable' : 'disable') . $property .'();'."\n";
	}
	
	function get_placeholder($width, $height, $additional_styles = '') {
		if (!$this->init_done) {
			trigger_error('Prima di richiamare la funzione get_placeholder devi richiamare init()', E_USER_ERROR);
			return '';
		}
		$out  = '<div id="'.$this->domElId .'" style="width: '. $width .'px; height: '. $height .'px; '. $additional_styles .'">';
		
		$out .= '<noscript>'."\n";
		$out .= '<img src="http://maps.google.com/staticmap?';
		
		$out .= implode('&', $this->_get_static_parameters($width, $height));
		
		$out .= '" width="'.$width.'" height="'.$height.'" />';
		$out .= '</noscript>'."\n";
		
		$out .= '</div>';
		
		return $out;
	}
	
	function _get_static_parameters($w, $h) {
		$out = $markers = array();
		$out[] = 'center='.$this->center_lat.','.$this->center_lng;
		$out[] = 'zoom='. $this->zoom;
		$out[] = 'size='. $w .'x'. $h;
		foreach($this->markers as $m) {
			$markers[] = $m[0] .",". $m[1];
		}
		if (count($markers) > 0) {
			$out[] = 'markers='. implode('|', $markers);
		}
		return $out;
	}
	
}

?>